package com.zhentao.config.cronTwo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.config.cronOne.CronTaskFoo;
import com.zhentao.domain.*;
import com.zhentao.domain.dto.CycleDailyExams;
import com.zhentao.domain.dto.DailyTime;
import com.zhentao.mapper.*;
import com.zhentao.service.impl.SExaminationInformationServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.*;
import java.util.*;

@Service
public class CronTaskFooTwo implements IPollableServiceTwo {

    @Resource
    private SExaminationInformationServiceImpl service;

    @Resource
    private SExaminationInformationMapper sExaminationInformationMapper;

    @Resource
    private SClassMapper sClassMapper;

    @Resource
    private SQuestionMapper sQuestionMapper;

    @Resource
    private STestPaperMapper sTestPaperMapper;

    @Resource
    private SSubjectMapper sSubjectMapper;

    @Resource
    private RedisTemplate<String,String> redisTemplate;


    CronTaskFoo foo = new CronTaskFoo();
    Integer d = foo.d;

    @Override
    public void poll() {
//        String s = "08:30";
//        LocalDate date = LocalDate.now().plusDays(1);
//        LocalTime time = LocalTime.parse(s);
//        LocalDateTime dateTime = LocalDateTime.of(date, time);
//        System.out.println(dateTime);

        LocalDate currentDate = LocalDate.now();
        if (currentDate.getDayOfWeek() != DayOfWeek.SATURDAY) {
            CycleDailyExams cycleDailyExams = service.returnCycleDailyExams();
            Integer[] classId = cycleDailyExams.getClassId();
            DailyTime[] beginEndTime = cycleDailyExams.getBeginEndTime();
            for (int i = 0; i < classId.length; i++) {
                //每个班级的考试时间
                DailyTime dailyTimes = beginEndTime[i];
                //生成班级日考
                SExaminationInformation sExaminationInformation = new SExaminationInformation();
                SClass sClass = this.sClassMapper.selectById(classId[i]);
                //获取到班级的阶段
                Integer lpId = sClass.getLpId();
                //找到这个阶段对应的所有课程
                QueryWrapper<SSubject> queryWrapper = new QueryWrapper<SSubject>();
                queryWrapper.lambda().eq(SSubject::getLpId, lpId);
                List<SSubject> list = this.sSubjectMapper.selectList(queryWrapper);
                SSubject sSubject = new SSubject();

                if (d < list.size()) {
                    sSubject = list.get(d);
                    // ...
                } else {
                    System.err.println("考试单元已用完");
                }
                //这场考试的开始结束时间
                String beginTime = dailyTimes.getTestBeginTime();
                String endTime = dailyTimes.getTestEndTime();
                LocalDate date = LocalDate.now().plusDays(1);
                LocalTime time = LocalTime.parse(beginTime);
                LocalDateTime beginTime1 = LocalDateTime.of(date, time);
                LocalDate date1 = LocalDate.now().plusDays(1);
                LocalTime time1 = LocalTime.parse(endTime);
                LocalDateTime endTime1 = LocalDateTime.of(date1, time1);

                //考试的时长
                Duration duration = Duration.between(beginTime1, endTime1);
                long l = duration.toMinutes();
                Date date2 = new Date();
                date2.setMinutes((int) l);
                date2.setHours(0);
                date2.setSeconds(0);

                sExaminationInformation.setName(sClass.getClassName() + sSubject.getSubjectName() + "日考");
                sExaminationInformation.setBeginTime(Date.from(beginTime1.atZone(ZoneId.systemDefault()).toInstant()));
                sExaminationInformation.setEndTime(Date.from(endTime1.atZone(ZoneId.systemDefault()).toInstant()));
                sExaminationInformation.setUId(1);
                sExaminationInformation.setSubId(sSubject.getId());
                sExaminationInformation.setRadioCount("10_1");
                sExaminationInformation.setCheckCount("10_1");
                sExaminationInformation.setJudgmentCount("10_1");
                sExaminationInformation.setTextCount("10_5");
                sExaminationInformation.setExamDuration(date2);
                sExaminationInformation.setClassId(classId[i]);
                this.sExaminationInformationMapper.insert(sExaminationInformation);
                redisTemplate.opsForList().leftPush("examinationInfo",sExaminationInformation.getId()+"_"+sExaminationInformation.getBeginTime()+"_"+sExaminationInformation.getEndTime()+"_"+0);
                this.createTestPaper(sExaminationInformation.getId());
            }
            if (d != 19) {
                d++;
            } else {
                d = 0;
            }
        }
    }
 
    @Override
    public String getCronExpression() {
        return service.returnCronTwo();
    }


    //生成试卷  参数为考试的id
    public void createTestPaper(Integer id) {
        //根据id查找这场考试
        SExaminationInformation sExaminationInformation = this.sExaminationInformationMapper.selectById(id);
        //单选题数量
        String radioCount = sExaminationInformation.getRadioCount();
        String[] s = radioCount.split("_");
        int radioCount1 = Integer.parseInt(s[0]);
        //多选题数量
        String checkCount = sExaminationInformation.getCheckCount();
        String[] s1 = checkCount.split("_");
        int checkCount1 = Integer.parseInt(s1[0]);
        //判断题数量
        String judgmentCount = sExaminationInformation.getJudgmentCount();
        String[] s2 = judgmentCount.split("_");
        int judgmentCount1 = Integer.parseInt(s2[0]);
        //填空题数量
        String textCount = sExaminationInformation.getTextCount();
        String[] s3 = textCount.split("_");
        int textCount1 = Integer.parseInt(s3[0]);
        //获取这场考试阶段对应的题目
        QueryWrapper<SQuestion> queryWrapper = new QueryWrapper<SQuestion>();
        queryWrapper.eq("sub_id", sExaminationInformation.getSubId());
        queryWrapper.eq("state",0);
        List<SQuestion> list = this.sQuestionMapper.selectList(queryWrapper);
        //所有单选题列表
        List<SQuestion> radioList = new ArrayList<>();
        //所有多选题列表
        List<SQuestion> checkList = new ArrayList<>();
        //所有判断题列表
        List<SQuestion> judgmentList = new ArrayList<>();
        //所有简答题列表
        List<SQuestion> textList = new ArrayList<>();
        for (SQuestion sQuestion : list) {
            if (sQuestion.getTypeId() == 1) {
                radioList.add(sQuestion);
            } else if (sQuestion.getTypeId() == 2) {
                checkList.add(sQuestion);
            } else if (sQuestion.getTypeId() == 3) {
                judgmentList.add(sQuestion);
            } else if (sQuestion.getTypeId() == 4) {
                textList.add(sQuestion);
            }
        }
        //要添加的单选题
        Set<SQuestion> radioset = new HashSet<>();
        //要添加的多选题
        Set<SQuestion> checkset = new HashSet<>();
        //要添加的判断题
        Set<SQuestion> judgmentset = new HashSet<>();
        //要添加的简答题
        Set<SQuestion> textset = new HashSet<>();
        //添加到单选set中
        if (radioList.isEmpty()){

        }else {
            for (int i = 0; i < radioCount1; i++) {
                Random random = new Random();
                int index = random.nextInt(radioList.size());
                SQuestion sQuestion = radioList.get(index);
                radioset.add(sQuestion);
            }
        }
        //添加到多选set中
        if (checkList.isEmpty()){

        }else {
            for (int i = 0; i < checkCount1; i++) {
                Random random = new Random();
                int index = random.nextInt(checkList.size());
                SQuestion sQuestion = checkList.get(index);
                checkset.add(sQuestion);
            }
        }
        //添加到判断题set中
        if (judgmentList.isEmpty()){

        }else {
            for (int i = 0; i < judgmentCount1; i++) {
                Random random = new Random();
                int index = random.nextInt(judgmentList.size());
                SQuestion sQuestion = judgmentList.get(index);
                judgmentset.add(sQuestion);
            }
        }
        //添加到简答题set中
        if (textList.isEmpty()){

        }else {
            for (int i = 0; i < textCount1; i++) {
                Random random = new Random();
                int index = random.nextInt(textList.size());
                SQuestion sQuestion = textList.get(index);
                textset.add(sQuestion);
            }
        }
        //添加单选题到试卷
        for (SQuestion sQuestion : radioset) {
            STestPaper sTestPaper = new STestPaper();
            sTestPaper.setQuestionId(sQuestion.getId());
            sTestPaper.setTestinfoId(id);
            this.sTestPaperMapper.insert(sTestPaper);
        }
        //添加多选题到试卷
        for (SQuestion sQuestion : checkset) {
            STestPaper sTestPaper = new STestPaper();
            sTestPaper.setQuestionId(sQuestion.getId());
            sTestPaper.setTestinfoId(id);
            this.sTestPaperMapper.insert(sTestPaper);
        }
        //添加判断题到试卷
        for (SQuestion sQuestion : judgmentset) {
            STestPaper sTestPaper = new STestPaper();
            sTestPaper.setQuestionId(sQuestion.getId());
            sTestPaper.setTestinfoId(id);
            this.sTestPaperMapper.insert(sTestPaper);
        }
        //添加简答题到试卷
        for (SQuestion sQuestion : textset) {
            STestPaper sTestPaper = new STestPaper();
            sTestPaper.setQuestionId(sQuestion.getId());
            sTestPaper.setTestinfoId(id);
            this.sTestPaperMapper.insert(sTestPaper);
        }
    }
}